package com.itelite.controller;

import com.itelite.common.info.Result;
import com.itelite.common.util.FileUtils;
import com.itelite.domain.pojo.Prize;
import com.itelite.handler.ValidateHandler;
import com.itelite.service.PrizeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 沈洋 邮箱:1845973183@qq.com
 * @create 2021/8/6-8:34
 **/
@Slf4j
@Api("获奖记录模块")
@RestController
@RequestMapping("/prize/api")
public class PrizeController {
    @Autowired
    private PrizeService prizeService;

    @ApiOperation("上传获奖记录")
    @PostMapping("/uploadPrizeRecord")
    public Result uploadPrizeRecord(@RequestBody @Validated Prize prize, BindingResult error){
        if (error.hasErrors()) {
            return Result.failure(ValidateHandler.catchErrors(error));
        }
        int i = 0;
        if ((i = prizeService.uploadPrizeRecord(prize))>0) {
            return Result.success("上传成功，等待组长审核").build().data("prizeId",i);
        }
        return Result.failure();
    }

    @ApiOperation("上传获奖资料（只能上传zip、7Z文件）")
    @PostMapping("/uploadPrizeFile")
    public Result uploadPrizeFile(@RequestParam Integer prizeId, @RequestParam(required = true) MultipartFile file){
        if (!FileUtils.zipExtensionName.contains(file.getContentType())) {
            return Result.failure("文件格式错误");
        }
        if (prizeService.uploadPrizeFile(prizeId,file)) {
            return Result.success();
        }return Result.failure();
    }
}
